//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.mall.controller;

import com.example.mall.common.api.CommonPage;
import com.example.mall.common.api.CommonResult;
import com.example.mall.dto.WeightDto;
import com.example.mall.mbg.model.*;
import com.example.mall.service.IndicatorService;
import com.example.mall.service.SupplierService;
import com.example.mall.service.WeightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Api(
        tags = {"WeightController"},
        description = "权重管理"
)
@Controller
@RequestMapping({"/weight"})
public class WeightController {
    @Autowired
    private WeightService weightService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private IndicatorService indicatorService;
    private static final Logger LOGGER = LoggerFactory.getLogger(WeightController.class);

    public WeightController() {
    }

    @ApiOperation("画面：权重画面")
    @RequestMapping(value = {"init"},method = {RequestMethod.GET})
    public String init(Model model, HttpSession session) {
        List<WeightDto> weightDtos = new ArrayList();
        List<Weight> weights = this.weightService.listAll();
        weights.forEach((weight) -> {
            WeightDto weightDto = new WeightDto(weight);
            Supplier supplier = this.supplierService.get(weight.getSupplierid());
            weightDto.setSupplier(supplier);
            Indicator indicator = this.indicatorService.get(weight.getIndicatorid());
            weightDto.setIndicator(indicator);
            weightDtos.add(weightDto);
        });
        model.addAttribute("weightDtos", weightDtos);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "weight-list";
    }

    @ApiOperation("获取所有权重列表")
    @RequestMapping(
            value = {"listAll"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<List<WeightDto>> getWeightList() {
        List<WeightDto> weightDtos = new ArrayList();
        List<Weight> weights = this.weightService.listAll();
        weights.forEach((weight) -> {
            WeightDto weightDto = new WeightDto(weight);
            Supplier supplier = this.supplierService.get(weight.getSupplierid());
            weightDto.setSupplier(supplier);
            Indicator indicator = this.indicatorService.get(weight.getIndicatorid());
            weightDto.setIndicator(indicator);
            weightDtos.add(weightDto);
        });
        return CommonResult.success(weightDtos);
    }

    @ApiOperation("添加权重")
    @RequestMapping(
            value = {"/create"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult createWeight(@RequestBody Weight weight) {
        int count = this.weightService.create(weight);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success(weight);
            LOGGER.debug("createWeight success:{}", weight);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createWeight failed:{}", weight);
        }

        return commonResult;
    }

    @ApiOperation("画面：权重编辑")
    @RequestMapping(value = {"edit"},method = {RequestMethod.GET})
    public String edit(Model model, Integer weightid, HttpSession session) {
        Weight weight = weightService.get(weightid);
        List<Supplier> supplierList = supplierService.listAll();
        List<Indicator> indicatorList = indicatorService.listAll();
        model.addAttribute("weight", weight);
        model.addAttribute("supplierList", supplierList);
        model.addAttribute("indicatorList", indicatorList);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "weight-detail";
    }

    @ApiOperation("更新指定id权重信息")
    @RequestMapping(value = {"/update/{id}"}, method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult updateWeight(@PathVariable("id") Integer weightid, @RequestBody Weight weight, BindingResult bindingResult) {
        weight.setUpdatetime(new Date());
        int count = this.weightService.update(weightid, weight);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success(weight);
            LOGGER.debug("updateWeight success:{}", weight);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateWeight failed:{}", weight);
        }

        return commonResult;
    }

    @ApiOperation("删除指定id的权重")
    @RequestMapping(
            value = {"/delete/{id}"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult deleteWeight(@PathVariable("id") Integer weightid) {
        int count = this.weightService.delete(weightid);
        CommonResult commonResult;
        if (count == 1) {
            commonResult = CommonResult.success((Object)null);
            LOGGER.debug("deleteWeight success:id={}", weightid);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("deleteWeight failed:id={}", weightid);
        }

        return commonResult;
    }

    @ApiOperation("分页查询权重列表")
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<CommonPage<Weight>> listWeight(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize) {
        List<Weight> weightList = this.weightService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(weightList));
    }

    @ApiOperation("获取指定id的权重详情")
    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public CommonResult<Weight> weight(@PathVariable("id") Integer weightid) {
        return CommonResult.success(this.weightService.get(weightid));
    }
}
